﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Xml.Linq;
using NextCMS.Models;
using System.IO;

namespace NextCMS.DataAccess.XMLDataAccess
{
    internal class SiteResources : ISiteResource
    {
        private static string FilePath = HttpContext.Current.Server.MapPath("~") + "/App_Data/SiteResources.xml";
        string Descendants = "SiteResource";
        XDocument doc = null;
        public SiteResources()
        {
            doc = XDocument.Load(FilePath);  //load the xml file.
        }
        public SiteResourceModel GetById(int Id)
        {

            var q = from d in doc.Descendants(Descendants)
                    where (int)d.Element("Id") == Id
                    select new SiteResourceModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        Name = d.Element("Name").Value,
                        FolderName = d.Element("FolderName").Value
                    };

            return q.FirstOrDefault();
        }

        public IEnumerable<SiteResourceModel> GetAllBySiteId(int SiteId)
        {

            var q = from d in doc.Descendants(Descendants)
                    where (int)d.Element("SiteId") == SiteId
                    select new SiteResourceModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        Name = d.Element("Name").Value,
                        FolderName = d.Element("FolderName").Value
                    };

            return q;
        }
        public IEnumerable<SiteResourceModel> GetAll()
        {

            var q = from d in doc.Descendants(Descendants)
                    select new SiteResourceModel
                    {
                        Id = (int)d.Element("Id"),
                        SiteId = (int)d.Element("SiteId"),
                        Name = d.Element("Name").Value,
                        FolderName = d.Element("FolderName").Value
                    };

            return q;
        }
        public int Save(SiteResourceModel page)
        {

            var q = from d in doc.Descendants(Descendants)
                    select new SiteResourceModel
                    {
                        Id = (int)d.Element("Id"),
                    };
           

            if (q.Count() > 0)
                page.Id = q.Max(s => s.Id) + 1;
            else
                page.Id = 1;


            var oMember = new XElement(Descendants,
             new XElement("Id", page.Id),
             new XElement("SiteId", page.SiteId),
             new XElement("Name", page.Name),
             new XElement("FolderName", page.FolderName)
             );

            doc.Root.Add(oMember);
            doc.Save(FilePath);
            return page.Id;
        }
        // Delete Record
        public void Delete(int id)
        {
            SiteResourceModel sm=this.GetById(id);
            //Directory.Delete(HttpContext.Current.Server.MapPath("~") + "/Templates/" + sm.FolderName,true);
            doc.Root.Elements(Descendants).Where(i => (int)i.Element("Id") == id).Remove();
            doc.Save(FilePath);
        }

        // Edit Record
        public void Edit(SiteResourceModel page)
        {

            XElement node = doc.Root.Elements(Descendants).Where(i => (int)i.Element("Id") == page.Id).FirstOrDefault();
            node.SetElementValue("SiteId", page.SiteId);
            node.SetElementValue("Name", page.Name);
            node.SetElementValue("FolderName", page.FolderName);
            doc.Save(FilePath);
        }

        public void Dispose()
        {
            doc = null;
        }
    }
}
